using System;
using System.Linq;
using System.Xaml;
using AngryPig.Data;
using Microsoft.Xna.Framework.Content.Pipeline;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace AngryPig.Pipeline
{
    [ContentImporter(".xaml", DisplayName = "AngryPig Level Importer")]
    public class LevelImporter : ContentImporter<LevelInfo>
    {
        public override LevelInfo Import(string filename, ContentImporterContext context)
        {
            var level = new LevelInfo();
            var canvas = (Canvas)XamlServices.Load(filename);

            level.Bricks = ((Canvas)canvas.Children[0]).Children.OfType<Rectangle>().Select(rc => new OrientedBox()
            {
                Rotation = 0,
                Position = new Microsoft.Xna.Framework.Vector2((float)Canvas.GetLeft(rc), (float)Canvas.GetTop(rc)),
                Size = new Microsoft.Xna.Framework.Vector2((float)rc.Width, (float)rc.Height),
            }).ToArray();
            
            level.Birds = ((Canvas)canvas.Children[0]).Children.OfType<Ellipse>().Select(rc => 
                new Microsoft.Xna.Framework.Vector2((float)Canvas.GetLeft(rc), (float)Canvas.GetTop(rc))
            ).ToArray();

            return level;
        }
    }
}